package com.ss.android.action;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.ss.android.account.b.ActionThread2;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.ActionData;
import com.ss.android.account.model.SharePlatform;
import com.ss.android.article.browser.R;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.AbsArticle;

import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ArticleActionHelper implements Callback {
    public final Context a;
    public final Handler b;
    private Spipe c;
    private WeakReference<Activity> d;
    private HashSet<String> e;

    public ArticleActionHelper(Context arg3) {

        this.b = new WeakHandler(((Callback) this));
        this.a = arg3.getApplicationContext();
        if ((arg3 instanceof Activity)) {
            this.d = new WeakReference(arg3);
        }

        this.c = Spipe.inst();
        arg3.getString(com.ss.android.article.browser.R.string.ss_action_digg_fmt);
        arg3.getString(com.ss.android.article.browser.R.string.ss_action_bury_fmt);
        arg3.getString(com.ss.android.article.browser.R.string.ss_action_digg_exist);
        arg3.getString(com.ss.android.article.browser.R.string.ss_action_bury_exist);
        this.e = new HashSet();
        this.e.add("sina_weibo");
        this.e.add("qq_weibo");
        this.e.add("renren_sns");
        this.e.add("kaixin_sns");
    }

    private void a(boolean arg13, ActionData arg14) {
        int v1;
        boolean v3 = false;
        boolean v2 = true;
        if (arg14 != null && arg14.mItem != null && (arg13)) {
            int v5 = Spipe.c(arg14.mAction);
            if (v5 <= 0) {
                return;
            }

            AbsArticle v6 = arg14.mItem;
            if (arg14.mPlats != null && !arg14.mPlats.isEmpty()) {
                if (arg14.mRepostSuccess) {
                    HashSet v7 = new HashSet();
                    if (!android.support.design.a.isTextEmpty(arg14.mExpiredPlatform)) {
                        try {
                            String[] v0_1 = arg14.mExpiredPlatform.split(",", -1);
                            if (v0_1 == null) {
                            } else {
                                ((Set) v7).addAll(Arrays.asList(((Object[]) v0_1)));
                            }
                        } catch (Exception v0) {
                        }

                        if (v6 != null && this.c != null && this.d != null && this.d.get() != null && (android.support.design.a.isComponentActive(this.d.get()))) {
                            this.c.a(arg14.mExpiredPlatform, this.d.get());
                        }
                        v1 = 1;
                    } else {
                        v1 = 0;
                    }

                    StringBuffer v8 = new StringBuffer();
                    Iterator v9 = arg14.mPlats.iterator();
                    int v4 = 1;
                    while (v9.hasNext()) {
                        Object v0_2 = v9.next();
                        if (((Set) v7).contains(((SharePlatform) v0_2).mName)) {
                            continue;
                        }

                        if (v4 == 0) {
                            v8.append(this.a.getString(com.ss.android.article.browser.R.string.ss_send_success_delimiter));
                        } else {
                            v4 = 0;
                        }

                        v8.append(this.a.getString(((SharePlatform) v0_2).mVerbose));
                    }
                    UIUtils.showImageToast(this.a, R.drawable.doneicon_popup_textpage, String.format(this.a.getString(com.ss.android.article.browser.R.string.ss_send_success_pattern), v8));
                } else if (arg14.mPostError == 105) {
                    v1 = 1;
                } else if (arg14.mPostError == 108) {
                    if (v6 != null && this.c != null && this.d != null && this.d.get() != null && !android.support.design.a.isTextEmpty(arg14.mExpiredPlatform) && (android.support.design.a.isComponentActive(this.d.get()))) {
                        this.c.a(arg14.mExpiredPlatform, this.d.get());
                    }
                    v1 = 1;
                } else {
                    v1 = 0;
                }

                label_80:
                if (v1 != 0) {
                    this.c.b(this.a);
                }
            }

            if (!arg14.mSuccess) {
                com.ss.android.action.bb.a();
                com.ss.android.action.bb.a(v5, arg14.mTimeMillis, v6, false);
                return;
            }

            if (arg14.mDiggCount >= 0) {
                if (v6.diggCount < arg14.mDiggCount) {
                    v6.diggCount = arg14.mDiggCount;
                }

                v3 = true;
            }

            if (arg14.mBuryCount >= 0) {
                if (v6.buryCount < arg14.mBuryCount) {
                    v6.buryCount = arg14.mBuryCount;
                }

                v3 = true;
            }

            if (arg14.mLikeCount >= 0) {
                if (v6.likeCount < arg14.mLikeCount) {
                    v6.likeCount = arg14.mLikeCount;
                }

                v3 = true;
            }

            if (arg14.mRepinCount >= 0) {
                v6.repinCount = arg14.mRepinCount;
                if ((v6.userRepin) && v6.repinCount <= 0) {
                    v6.repinCount = 1;
                }

                v3 = true;
            }

            if (arg14.mCommentCount >= 0) {
                v6.commentCount = arg14.mCommentCount;
            } else {
                v2 = v3;
            }

            com.ss.android.action.bb.a();
            com.ss.android.action.bb.a(v5, arg14.mTimeMillis, v6, v2);
        }
    }

    public final void a(int arg8, AbsArticle arg9) {
        this.a(arg8, arg9, 0, null);
    }

    public final void a(int arg12, AbsArticle absArticle, long arg14, List<SharePlatform> arg16) {
        String v3 = Spipe.a(arg12);
        if (!android.support.design.a.isTextEmpty(v3) && absArticle != null) {
            long v4 = System.currentTimeMillis();
            com.ss.android.action.bb.a();
            com.ss.android.action.bb.a(arg12, v4, absArticle);
            if (arg16 != null && arg16.size() > 0 && this.e != null) {
                Iterator<SharePlatform> iterator = arg16.iterator();
                while (iterator.hasNext()) {
                    if (this.e.contains(iterator.next().mName)) {
                        continue;
                    }

                    iterator.remove();
                }
            }

            if (!NetworkUtils.isNetworkAvailable(this.a)) {
                return;
            }

            new ActionThread2(this.a, this.b, v3, v4, absArticle, arg14, arg16).start();
        }
    }

    public void a(int arg8, AbsArticle arg9, long arg10) {
        this.a(arg8, arg9, arg10, null);
    }

    public void handleMsg(Message message) {
        List v7 = null;
        boolean v1 = true;
        switch (message.what) {
            case 1005: {
                if (!(message.obj instanceof ActionData)) {
                    return;
                }
                this.a(v1, (ActionData) message.obj);
                break;
            }
            case 1006: {
                v1 = false;
                if (!(message.obj instanceof ActionData)) {
                    return;
                }
                this.a(v1, (ActionData) message.obj);
                break;
            }
            case 1033: {
                if (!(message.obj instanceof com.ss.android.account.b.f)) {
                    return;
                }

                int v5 = v7.size();
                int v4;
                for (v4 = 0; v4 < v5; ++v4) {
                    Object v0 = v7.get(v4);
                    boolean v3 = ((ActionData) v0).mMsgValue == 1005 ? true : false;
                    this.a(v3, ((ActionData) v0));
                }
            }
        }
    }
}

